$(function(){
	var dataTable = $("#bpUserDataTable").DataTable({
		"ajax": {
			"url": '/bpUser/list',
			"data": function(data){
				data.nickName = $("#nickName").val();
				data.phone = $("#phone").val();
				data.sex = $("#sex").val();
				data.province = $("#province").val();
				data.city = $("#city").val();
				data.county = $("#county").val();
				data.userType = $("#userType").val();
				data.userStatus = $("#userStatus").val();
				data.registerType = $("#registerType").val();
			}
		},
		"ordering": false,
		"columns": [
//			{
//				"data": "id",
//				"className": "text-center sign-col",
//				"render": function(data, type, row, meta){
//					return '<input type="checkbox" name="pk" value="' + data + '" onclick="checkOne()"/>';
//				}
//			},
			{
				"data": function(obj){
					var text = '';
					if (obj.userPhoto != null) {
						return '<img alt="" src="http://39.105.119.87:8088'+obj.userPhoto+'" width="40" height="40"/>';
					} else {
						text = '-';
					}
					return text;
				}
			},
			{
				"data": function(obj){
					var text = '';
					if (obj.nickName != null) {
						text = obj.nickName;
					} else {
						text = '-';
					}
					return text;
				}
			},
			{
				"data": function(obj){
					var text = '';
					if (obj.sex == '1') {
						text = '男';
					} else if (obj.sex == '2') {
						text = '女';
					} else {
						text = '-';
					}
					return text;
				}
			},
//			{"data": "province"},
//			{"data": "city"},
//			{"data": "county"},
			{
				"data": function(obj){
					var text = '';
					if (obj.userType == '1') {
						text = '普通用户';
					} else if (obj.userType == '2') {
						text = '会员';
					} else {
						text = '游客';
					}
					return text;
				}
			},
			{
				"data": function(obj){
					var text = '';
					if (obj.userType == '0') {
						text = '注销';
					} else if (obj.userType == '1') {
						text = '正常';
					} else if (obj.userType == '2') {
						text = '封禁';
					} else if (obj.userType == '3') {
						text = '删除';
					}
					return text;
				}
			},
			{
				"data": function(obj){
					var text = '';
					if (obj.registerType == '0') {
						text = '管理员注册';
					} else if (obj.registerType == '1') {
						text = 'Android';
					} else if (obj.registerType == '2') {
						text = 'IOS';
					}
					return text;
				}
			},
			{
				"data": "id",
				"className": "operate-col",
				"data": function(data){
//					var html = '<button type="button" onclick="edit(\''+ data +'\')" class="btn btn-sm btn-warning right-5"><i class="fa fa-edit"></i></button>'
					var html = '<button type="button" onclick="view(\''+ data.id +'\')" class="btn btn-sm btn-info"><i class="fa fa-search"></i></button>';
					if(data.userStatus == '1'){
						html += '<button type="button" onclick="changeStatus(\''+ data.id +'\', \'2\')" class="btn btn-sm btn-info">封禁</button>';
					} else {
						html += '<button type="button" onclick="changeStatus(\''+ data.id +'\', \'1\')" class="btn btn-sm btn-info">解封</button>';
					}
					return html;
				}
			}
		]
	});
	
	$('#searchBtn').click(function(){
		dataTable.ajax.reload();
	});
	
	$('#resetBtn').click(function(){
		$('#bpUserSearchForm')[0].reset();
		dataTable.ajax.reload();
	});
	
	$('#addBtn').click(function(){
		forwardPage('/bpUser/bpUser');
	});
	
//	$('#delBtn').click(function(){
//		ajaxBySelecteRows(dataTable,'/bpUser/delete','确定要删除？');
//	});
	
	// 日期
	$('.datepicker').datetimepicker({
		language: 'zh-CN',
		format: 'yyyy-mm-dd',
		autoclose: true,
		todayBtn: true,
		clearBtn:true,
		minView: 2
	});
});

function edit(id){
	forwardPage('/bpUser/bpUser?id='+id);
}

function view(id){
	forwardPage('/bpUser/view?id='+id);
}
function changeStatus(id, status){
	$.ajax({
		type: 'post',
		url: '/bpUser/changeStatus',
		data: {id: id, userStatus: status},
		dataType: 'json',
		success: function(response){
			modalAlert(response.message);
			$("#bpUserDataTable").DataTable().ajax.reload();
		}
	});
}